package Test;

import org.junit.jupiter.api.Test;

import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Util {
    public static ArrayList<String> findImageFiles(String path) {
        ArrayList<String> imageFiles = new ArrayList<>();
        File directory = new File(path);

        if (directory.exists()) {
            File[] files = directory.listFiles();

            for (File file : files) {
                if (file.isDirectory()) {
                    imageFiles.addAll(findImageFiles(file.getAbsolutePath()));
                } else {
                    String fileName = file.getName().toLowerCase();
                    if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg") ||
                            fileName.endsWith(".png") || fileName.endsWith(".gif")) {
                        imageFiles.add(file.getAbsolutePath());
                    }
                }
            }
        }

        return imageFiles;
    }
    public static String getPublicIP() {
        // 这里使用了ipify服务，你可以根据需要替换为其他服务
        String ipService = "http://api.ipify.org";

        try (Scanner scanner = new Scanner(new URL(ipService).openStream(), "UTF-8")) {
            // 使用Scanner读取服务返回的数据
            return scanner.useDelimiter("\\A").next();
        } catch (Exception e) {
            // 处理异常，比如网络问题或服务不可达
            e.printStackTrace();
            return "无法获取公网IP地址";
        }
    }
    public static List<String> splitString(String input) {
        String delimiter = "->";
        List<String>result= List.of(input.substring(1).split(delimiter));
        return result;
    }
    public byte[] readFileToByteArray(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException("File not found: " + filePath);
        }

        try (InputStream inputStream = new FileInputStream(file)) {
            long fileSize = file.length();
            if (fileSize > Integer.MAX_VALUE) {
                throw new IOException("File is too large");
            }

            byte[] bytes = new byte[(int) fileSize];
            int offset = 0;
            int numRead;
            while (offset < bytes.length && (numRead = inputStream.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += numRead;
            }

            if (offset < bytes.length) {
                throw new IOException("Could not completely read file " + file.getName());
            }

            return bytes;
        }
    }
    public static byte[] checkBinaryArray(String input) {
        String[] parts = input.split(", ");
        byte[] byteArray = new byte[parts.length];

        for (int i = 0; i < parts.length; i++) {
            try {
                byteArray[i] = Byte.parseByte(parts[i]);
            } catch (NumberFormatException e) {
                return null;
            }
        }

        return byteArray;
    }
    public static byte[] parseBinaryArray(String input) {
        String[] parts = input.split(", ");
        byte[] byteArray = new byte[parts.length];

        for (int i = 0; i < parts.length; i++) {
            try {
                byteArray[i] = Byte.parseByte(parts[i].replaceAll("\\[", "").replaceAll("]", ""));
            } catch (NumberFormatException e) {
                return null;
            }
        }

        return byteArray;
    }
//    @Test
//    public void M(){
//        byte[] bytes = checkBinaryArray("[ -106, 99, 117, -71, 110, -95, 0, 49, 46, 78, 51, -109, -48, -100, -1, 0, -98, -107, 109, -90, 12, -87, 116, -112, -101, -104, -36, -59, -122, -37, -122, 96, 120, 30, -107, 94, 11, 35, 102, -21, 37, -37, 112, -60, -19, -120, 49, -11, -11, -89, 90, -67, -83, -67, -6, -103, -18, 93, -111, 92, 16, 64, -50, 15, 56, -29, -67, 62, -10, -27, -92, 114, -58, 82, -91, -100, 48, -111, -57, -34, 61, -121, -27, 72, 77, -106, 100, -64, -98, 57, 2, 101, 72, -28, 3, -114, 113, -57, 52, -37, -58, -114, 125, -109, 66, -104, -3, -25, 37, 100, -56, 39, -95, 6, -94, -44, 30, 104, -74, -83, -69, 43, 43, 96, 74, -78, 103, 106, 14, 121, -4, -1, 0, -99, 87, -127, 110, 99, 10, -111, -78, -120, -68, -62, 50, 14, 70, 9, -11, -20, 104, 19, -109, -36, -66, 99, -123, -104, 69, 110, 51, -3, -18, -104, 62, -34, -58, -84, -23, -1, 0, 100, -75, -108, -4, -39, -115, -127, 82, -123, 49, -80, -25, -96, 53, -101, 44, -50, 38, -106, 53, 44, 20, -78, -104, -48, 116, 99, -33, -100, 118, 21, 53, -75, -28, 87, -118, -15, -85, -27, -42, 76, 51, 41, -63, 4, 113, -116, 81, -88, 94, 70, -10, -101, 119, 18, -37, -80, -127, 30, 72, -107, -68, -61, -71, -66, -23, -25, -90, 106, -60, 122, -5, 67, -74, 75, -119, 3, -7, -121, -127, -114, 0, -49, -75, 102, 105, -23, 48, -40, 86, 109, -15, -7, -89, 113, 3, -87, 29, 7, -67, 16, 94, 42, -56, -46, 121, 106, -50, -95, -126, -80, 28, 114, 113, -45, -4, -12, -93, 70, -62, -9, 58, 125, 53, -46, -26, 38, -1 ]");
//        System.out.println(bytes);
//    }

}
